<?php

namespace App\Kuafu\CustomerCoupon\Resources;

use App\Http\Resources\BaseResource;
use App\Kuafu\Activity\Enum\ActivityEnum;
use App\Kuafu\Coupon\Enum\CouponEnum;
use App\Kuafu\CustomerCoupon\Enum\CodeEnum;

class CustomCouponListResource extends BaseResource
{

    const STATUS_USED           = 1;
    const STATUS_STAY_EFFECTIVE = 3;
    const STATUS_STAY_FOR_USE   = 0;
    const STATUS_UN_EFFECTIVE   = 4;

    const STATUS_TEXT = [
        self::STATUS_STAY_FOR_USE   => "待使用",
        self::STATUS_USED           => "已使用",
        self::STATUS_STAY_EFFECTIVE => "待生效",
        self::STATUS_UN_EFFECTIVE   => "已失效"
    ];

    public function toArray($request)
    {
        return [
            "id"               => $this->id,
            "suitShop"         => $this->getSuitShop(),
            "uid"              => $this->userId,
            "username"         => objGetAttr($this->customers, "name", "-"),
            "mobile"           => objGetAttr($this->customers, "mobile", "-"),
            "receiveTime"      => formatDate($this->createdAt->timestamp),
            "useTime"          => formatDate($this->useTime, "-"),
            "couponId"         => $this->couponId,
            "couponName"       => $this->name,
            "couponType"       => $this->couponType,
            "couponTypeText"   => CouponEnum::COUPONTYPE[$this->couponType] ?? "-",
            "activityId"       => $this->activityId,
            "activityName"     => objGetAttr($this->activityRel, "name", "-"),
            "activityType"     => objGetAttr($this->activityRel, "type", -1),
            "activityTypeText" => ActivityEnum::TYPES[objGetAttr($this->activityRel, "type", -1)] ?? "-",
            "status"           => $this->getStatus(),
            "statusText"       => self::STATUS_TEXT[$this->getStatus()],
            "useStatus"        => $this->status,
            "useStartTime"     => formatDate($this->startAt),
            "useEndTime"       => formatDate($this->endAt),
        ]; // TODO: Change the autogenerated stub
    }


    /**
     * @return string
     */
    public function getSuitShop(): string
    {
        switch ($this->shopType) {
            case ActivityEnum::SHOP_TYPE_ALL:
            case ActivityEnum::SHOP_TYPE_FRANCHISE:
            case ActivityEnum::SHOP_TYPE_DIRECT:
                return ActivityEnum::SHOP_TYPES[$this->shopType];
                break;
            case ActivityEnum::SHOP_TYPE_SELECTION:
                return $this->couponShopRel->implode("shopRel.name", ",");
                break;
            default:
                return "-";
                break;
        }
    }

    /**
     * @return int
     */
    public function getStatus(): int
    {
        if ($this->status == CodeEnum::COUPON_STATUS_USED) {
            $status = self::STATUS_USED;
        } elseif ($this->startAt > time()) {
            $status = self::STATUS_STAY_EFFECTIVE;
        } elseif ($this->startAt < time() && $this->endAt > time()) {
            $status = self::STATUS_STAY_FOR_USE;
        } else {
            $status = self::STATUS_UN_EFFECTIVE;
        }
        return $status;
    }
}
